package com.cloud.business.controller.sys;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cloud.business.controller.BaseController;
import com.cloud.business.entity.sys.SysUser;
import com.cloud.business.model.commonvm.DataDropDown;
import com.cloud.business.model.commonvm.DeletePost;
import com.cloud.business.model.commonvm.ResultMsg;
import com.cloud.business.model.sys.sysuservm.LoginPost;
import com.cloud.business.model.sys.sysuservm.SysUserModel;
import com.cloud.business.model.sys.sysuservm.SysUserQueryModel;
import com.cloud.business.model.sys.sysuservm.UpdPwdPost;
import com.cloud.business.service.sys.ISysUserService;
import com.cloud.core.common.HttpContentResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 系统用户 前端控制器
 * </p>
 *
 * @author dh
 * @since 2020-03-21
 */
@Api(tags = "系统用户模块")
@RestController
@RequestMapping("/api/sysUser")
public class SysUserController extends BaseController {

    private final ISysUserService ISysUserService;

    @Autowired
    public SysUserController(ISysUserService ISysUserService) {
        this.ISysUserService = ISysUserService;
    }

    //region 获取分页列表

    /**
     * 获取分页列表
     *
     * @param queryModel
     * @return
     */
    @ApiOperation(value = "获取分页列表")
    @PostMapping("/getListByPage")
    public HttpContentResult getListByPage(@RequestBody SysUserQueryModel queryModel) {
        ResultMsg msg = new ResultMsg();
        try {
            queryModel.setOperationId(getOperationId());
            IPage<SysUserModel> page = new Page<>(queryModel.getPageNum(), queryModel.getRecordNum());
            ISysUserService.getListByPage(queryModel, page, msg);
            return HttpContentResult.successResult(msg.getMsg(), page.getTotal(), page.getRecords());
        } catch (Exception ex) {
            return HttpContentResult.errorResult(ex.getMessage());
        }
    }
    //endregion

    //region 创建

    /**
     * 创建
     *
     * @param model
     * @return
     * @author Rex
     */
    @ApiOperation(value = "创建")
    @PostMapping("/create")
    public HttpContentResult create(@RequestBody SysUserModel model) {
        ResultMsg msg = new ResultMsg();
        try {
            model.setCreateUserId(getOperationId());
            Integer objectId = ISysUserService.create(model, msg);
            return HttpContentResult.successResult(msg.getMsg(), 1L, objectId);
        } catch (Exception ex) {
            return HttpContentResult.errorResult(ex.getMessage());
        }
    }
    //endregion

    //region 更新

    /**
     * 更新
     *
     * @param model
     * @return
     * @author Rex
     */
    @ApiOperation(value = "更新")
    @PostMapping("/update")
    public HttpContentResult update(@RequestBody SysUserModel model) {
        ResultMsg msg = new ResultMsg();
        try {
            model.setModificationUserId(getOperationId());
            Integer objectId = ISysUserService.update(model, msg);
            return HttpContentResult.successResult(msg.getMsg(), 1L, objectId);
        } catch (Exception ex) {
            return HttpContentResult.errorResult(ex.getMessage());
        }
    }
    //endregion

    //region 获取单条

    /**
     * 获取单条
     *
     * @param objectId
     * @return
     */
    @ApiOperation(value = "获取单条")
    @GetMapping("/getById")
    public HttpContentResult getById(Integer objectId) {
        ResultMsg msg = new ResultMsg();
        try {
            Integer operationId = getOperationId();
            SysUser SysUser = ISysUserService.getById(operationId, objectId, msg);
            return HttpContentResult.successResult(msg.getMsg(), 1L, SysUser);
        } catch (Exception ex) {
            return HttpContentResult.errorResult(ex.getMessage());
        }
    }
    //endregion

    //region 批量删除

    /**
     * 批量删除
     *
     * @param deletePost
     * @return
     * @author Rex
     */
    @ApiOperation(value = "批量删除")
    @PostMapping("/deleteByList")
    public HttpContentResult deleteByList(@RequestBody DeletePost deletePost) {
        ResultMsg msg = new ResultMsg();
        try {
            deletePost.setOperationId(getOperationId());
            Integer count = ISysUserService.deleteByList(deletePost, msg);
            return HttpContentResult.successResult(msg.getMsg(), 1L, count);
        } catch (Exception ex) {
            return HttpContentResult.errorResult(ex.getMessage());
        }
    }
    //endregion

    //region 登陆

    /**
     * 登陆
     *
     * @param loginPost
     * @return
     */
    @ApiOperation(value = "登陆")
    @PostMapping("/login")
    public HttpContentResult login(@RequestBody LoginPost loginPost) {
        ResultMsg msg = new ResultMsg();
        try {
            SysUserModel sysUser = ISysUserService.login(loginPost, msg);
            return HttpContentResult.successResult(msg.getMsg(), 1L, sysUser);
        } catch (Exception ex) {
            return HttpContentResult.errorResult(ex.getMessage());
        }
    }
    //endregion

    //region 更新密码

    /**
     * 更新密码
     *
     * @param updPwdPost
     * @return
     */
    @ApiOperation(value = "更新密码")
    @PostMapping("/updPassword")
    public HttpContentResult updPassword(@RequestBody UpdPwdPost updPwdPost) {
        ResultMsg msg = new ResultMsg();
        try {
            updPwdPost.setOperationId(getOperationId());
            Integer objectId = ISysUserService.updPassword(updPwdPost, msg);
            return HttpContentResult.successResult(msg.getMsg(), 1L, objectId);
        } catch (Exception ex) {
            return HttpContentResult.errorResult(ex.getMessage());
        }
    }
    //endregion

    //region 重置密码

    /**
     * 重置密码
     *
     * @param objectId
     * @return
     */
    @ApiOperation(value = "重置密码")
    @GetMapping("/resetPwd")
    public HttpContentResult resetPwd(Integer objectId) {
        ResultMsg msg = new ResultMsg();
        try {
            Integer operationId = getOperationId();
            Integer objectIdRe = ISysUserService.resetPwd(operationId, objectId, msg);
            return HttpContentResult.successResult(msg.getMsg(), 1L, objectIdRe);
        } catch (Exception ex) {
            return HttpContentResult.errorResult(ex.getMessage());
        }
    }
    //endregion

    //region 按照角色/类型获取用户

    /**
     * 按照角色/类型获取用户
     *
     * @param roleCode
     * @param enumType
     * @return
     */
    @ApiOperation(value = "按照角色/类型获取用户")
    @GetMapping("/getListByEnumtype")
    public HttpContentResult getListByEnumtype(String roleCode, String enumType) {
        ResultMsg msg = new ResultMsg();
        try {
            Integer operationId = getOperationId();
            List<SysUser> sysUserList = ISysUserService.getListByEnumtype(operationId, roleCode, enumType, msg);
            return HttpContentResult.successResult(msg.getMsg(), 1L, sysUserList);
        } catch (Exception ex) {
            return HttpContentResult.errorResult(ex.getMessage());
        }
    }
    //endregion

    //region 根据角色code获取用户集合下拉

    /**
     * 根据角色code获取用户集合下拉
     *
     * @param roleCode
     * @return
     */
    @ApiOperation(value = "根据角色code获取用户集合下拉")
    @GetMapping("/getUsersByRoleCode")
    public HttpContentResult getUsersByRoleCode(String roleCode) {
        ResultMsg msg = new ResultMsg();
        try {
            Integer operationId = getOperationId();
            List<DataDropDown> sysUserList = ISysUserService.getUsersByRoleCode(operationId, roleCode, msg);
            return HttpContentResult.successResult(msg.getMsg(), 1L, sysUserList);
        } catch (Exception ex) {
            return HttpContentResult.errorResult(ex.getMessage());
        }
    }
    //endregion

    //region 获取下拉列表

    /**
     * 获取下拉列表
     *
     * @return
     */
    @ApiOperation(value = "获取下拉列表")
    @GetMapping("/getDropDownList")
    public HttpContentResult getDropDownList() {
        ResultMsg msg = new ResultMsg();
        try {
            Integer operationId = getOperationId();
            List<DataDropDown> dataDropDownList = ISysUserService.getDropDownList(operationId, msg);
            return HttpContentResult.successResult(msg.getMsg(), 1L, dataDropDownList);
        } catch (Exception ex) {
            return HttpContentResult.errorResult(ex.getMessage());
        }
    }
    //endregion
}
